package com.opdar.athena.client.agent.server;

import com.opdar.athena.client.agent.server.ajax.AjaxHandler;
import com.opdar.athena.client.agent.server.tcp.TcpDecoder;
import com.opdar.athena.client.agent.server.tcp.TcpEncoder;
import com.opdar.athena.client.agent.server.tcp.TcpHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * Created by shiju on 2017/6/27.
 */
public class AthenaInitializer extends ChannelInitializer<SocketChannel> {
    private final static TcpDecoder DECODER = new TcpDecoder();
    private final static TcpEncoder ENCODER = new TcpEncoder();
    private final static TcpHandler HANDLER = new TcpHandler();

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        pipeline.addLast(ENCODER);
        pipeline.addLast(new HttpResponseEncoder());
        pipeline.addLast(DECODER);
        pipeline.addLast(HANDLER);
        pipeline.addLast(new HttpRequestDecoder());
        pipeline.addLast(new HttpObjectAggregator(1048576));
        pipeline.addLast(new AjaxHandler());
    }
}
